﻿using System;
using UnityEngine;
using Fyber;
namespace com.yd.sdk.imp
{
    public enum BannerPos
    {
        Top,
        Bottom
    }

    public class FyberBehaviour : MonoBehaviour
    {
        private static FyberBehaviour _ins;
        public string APP_ID = "109613";
        public string USER_ID = "";
        
        [Header("预加载")]
        public bool isUseInterstitial = false;
        [Header("预加载")]
        public bool isUseReward = false;
        
        private string bannerPlacementId = "1111";//应该是随意的，可以支持多个，但是只弄一个
        private string interstitialPlacementId = "2222";//应该是随意的，可以支持多个，但是只弄一个
        private string rewardPlacementId = "3333";//应该是随意的，可以支持多个，但是只弄一个
        private bool _isShowBanner = false;

        public FyberBannerListener fyberBannerListener;
        public FyberInterstitialListener fyberInterstitialListener;
        public FyberRewardListener fyberRewardListener;
        
        
        void Awake()
        {
            _ins = this;
        }
        
        void Start()
        {
            //初始化
            FairBid.Start(APP_ID);

            if (isUseInterstitial)
            {
                Interstitial.Request(interstitialPlacementId);
                fyberInterstitialListener = new FyberInterstitialListener();
                Interstitial.SetInterstitialListener(fyberInterstitialListener);
            }



            if (isUseReward)
            {
                Rewarded.Request(rewardPlacementId);
                fyberRewardListener = new FyberRewardListener();
                Rewarded.SetRewardedListener(fyberRewardListener);
            }

            

        }
        
        /// <summary>
        /// 设置user_id
        /// </summary>
        /// <param name="user_id"></param>
        public void SetUserId(string user_id)
        {
            // recommended
            UserInfo.SetUserId(user_id);
        }
        
        /// <summary>
        /// 显示横幅广告
        /// </summary>
        /// <param name="position">"top","bottom"</param>
        public void ShowBanner(BannerPos position=BannerPos.Top)
        {
            if (_isShowBanner)
            {
                Banner.Destroy(bannerPlacementId);
                _isShowBanner = false;
            }
            BannerOptions bannerOptions = new BannerOptions();
            switch (position)
            {
                case BannerPos.Top:
                    bannerOptions.DisplayAtTheTop();
                    break;
                case BannerPos.Bottom:
                    bannerOptions.DisplayAtTheBottom();
                    break;
            }
            Banner.Show(bannerPlacementId,bannerOptions);
            _isShowBanner = true;
        }

        /// <summary>
        /// 显示插入广告
        /// </summary>
        public void ShowInterstitial()
        {
            if (Interstitial.IsAvailable(interstitialPlacementId))
                Interstitial.Show(interstitialPlacementId);
            else
                Interstitial.Request(interstitialPlacementId);
        }

        /// <summary>
        /// 显示激励广告
        /// OnCompletionDelegate
        /// </summary>
        public void ShowReward(FyberRewardListener.OnCompletionDelegate callback)
        {
            if (Rewarded.IsAvailable(rewardPlacementId))
                Rewarded.Show(rewardPlacementId);
            else
                Rewarded.Request(rewardPlacementId);

            fyberRewardListener.OnCompletionHandler = callback;
        }
        
        /// <summary>
        /// 感觉没有用
        /// </summary>
        public void AddBannerListener()
        {
            var listener = new FyberBannerListener();
            //TODO 设置回调
            Banner.SetBannerListener(listener);
        }

    }
}
